package com.hxzy.shop.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.shop.entity.MemberCard;
import com.hxzy.shop.entity.MemberPointChange;
import com.hxzy.shop.member.dto.MemberGrowUp.req.GrowUpValueReq;
import com.hxzy.shop.member.dto.MemberGrowUp.resp.GrowUpValueResp;
import com.hxzy.shop.member.dto.member.req.MemberDetailsReq;
import com.hxzy.shop.member.dto.member.req.MemberListsReq;
import com.hxzy.shop.member.dto.member.resp.MemberDetailResp;
import com.hxzy.shop.member.dto.member.resp.MemberLabelResp;
import com.hxzy.shop.member.dto.member.resp.MemberListsResp;
import com.hxzy.shop.entity.Member;
import com.hxzy.shop.member.mapper.LabelInnerMemberMapper;
import com.hxzy.shop.member.mapper.MemberCardMapper;
import com.hxzy.shop.member.mapper.MemberPointChangeMapper;
import com.hxzy.shop.member.service.MemberService;
import com.hxzy.shop.member.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author admin
* @description 针对表【member(会员表)】的数据库操作Service实现
* @createDate 2023-08-02 14:48:08
*/
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member>
    implements MemberService{

    /**
     * 会员表
     */
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 会员卡表
     */
    @Autowired
    private MemberCardMapper memberCardMapper;

    /**
     * 会员标签关联表
     */
    @Autowired
    private LabelInnerMemberMapper labelInnerMemberMapper;

    /**
     * 会员积分变更记录表
     */
    @Autowired
    private MemberPointChangeMapper memberPointChangeMapper;

    /**
     * 查看会员- 会员列表
     *
     * @param req 会员列表请求体
     * @return 查询的会员列表
     */
    @Override
    public PageInfo<MemberListsResp> memberLists(MemberListsReq req) {
        PageInfo<MemberListsResp> pageInfo = PageHelper.startPage(req.getPageNumber(),req.getPageSize())
                .doSelectPageInfo(() ->memberMapper.memberLists(req));
        List<MemberListsResp> list = pageInfo.getList();
        list.forEach(e ->{
            LambdaQueryWrapper<MemberCard> queryWrapper = new LambdaQueryWrapper<>();
            //计算积分的变化
            queryWrapper.ge(MemberCard::getEndValueOfGrowthRange,e.getCumulativePoints());
            queryWrapper.le(MemberCard::getStartValueOfGrowthRange,e.getCumulativePoints());

            MemberCard db = memberCardMapper.selectOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(db)){
                e.setMemberCarNumber(db.getId());
                e.setGradeName(db.getGradeName());
            }

            List<MemberLabelResp> memberLabel = labelInnerMemberMapper.getMemberLabel(new MemberDetailsReq(e.getMemberId(),0,0));

            e.setLabelName(memberLabel);

            MemberPointChange memberMaxPoint = memberPointChangeMapper.getMemberMaxPoint(e.getMemberId());

            if (ObjectUtil.isNotEmpty(memberMaxPoint)){
                e.setAccumulatedPoints(memberMaxPoint.getChangePointBak());
            }else {
                e.setAccumulatedPoints(0);
            }
        });

        pageInfo.setList(list);
        return pageInfo;
    }

    /**
     * 会员详细信息
     *
     * @param req 会员请求体
     * @return {@code MemberDetailResp}
     */
    @Override
    public MemberDetailResp memberDetails(MemberDetailsReq req) {
        //会员基本信息
        MemberDetailResp resp = memberMapper.findByLabelLists(req.getMemberId());
        //会员卡信息
        LambdaQueryWrapper<MemberCard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(MemberCard::getEndValueOfGrowthRange, resp.getGrowthValue());
        queryWrapper.le(MemberCard::getStartValueOfGrowthRange,resp.getGrowthValue());
        MemberCard db = memberCardMapper.selectOne(queryWrapper);
        if (db!=null){
            resp.setGradeName(db.getGradeName());
            resp.setMemberCarNumber(db.getId());
            //会员标签
            List<MemberLabelResp> memberLabel = labelInnerMemberMapper.getMemberLabel(new MemberDetailsReq(req.getMemberId(),0,0));
            resp.setLabelName(memberLabel);
        }else {
            //会员标签
            List<MemberLabelResp> label = labelInnerMemberMapper.getMemberLabel(new MemberDetailsReq(req.getMemberId(),0,0));
            resp.setLabelName(label);
        }
        return resp;
    }

    @Override
    public PageInfo<GrowUpValueResp> getMemberGrowUp(GrowUpValueReq req) {
        return null;
    }
}




